package rpc

import (
	"fmt"
	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/resolver"
	"log"
	"test.com/project-api/config"
	"test.com/project-common/discovery"
	"test.com/project-common/logs"
	"test.com/project-grpc/user/login"
)

// 定义全局rpc对象
var LoginServiceClient login.LoginServiceClient

// 拿到rpc的对象
func InitRpcUserClient() {

	//引入etcd
	etcdRegister := discovery.NewResolver(config.C.EtcdConfig.Addrs, logs.LG)
	resolver.Register(etcdRegister)
	conn, err := grpc.Dial("etcd:///user",
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		// 让grpc支持jaeger
		grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
	)

	//1、连接到server端口,此处禁用安全连接，没有加密和验证  拿到连接对象
	//conn, err := grpc.Dial("127.0.0.1:8881", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	fmt.Println("grpc连接成功")
	//这个报错   沃日！！！  卡我三天  艹！！！
	//defer conn.Close()
	//2、建立连接
	LoginServiceClient = login.NewLoginServiceClient(conn)
	//3、调用方法：这里的LoginServiceClient就可以直接访问到另一端的方法了
	//LoginServiceClient.GetCaptcha()
}
